package com.flame.system.monitor.controller;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.core.protocol.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@RestController
public class MonitorController {

    @HasAuth("server-monitor")
    @PostMapping("/system/monitor/server")
    public Result server() throws UnknownHostException {
        // 服务器名称
        String hostName = InetAddress.getLocalHost().getHostName();
        // 操作系统
        Properties properties = System.getProperties();
        String osName = properties.getProperty("os.name");
        // cpu物理核数
        SystemInfo systemInfo = new SystemInfo();
        int physicalProcessorCount = Runtime.getRuntime().availableProcessors();
        // 内存总大小
        long totalMemory = systemInfo.getHardware().getMemory().getTotal();
        // 交换区总大小
        long totalSwap = systemInfo.getHardware().getMemory().getVirtualMemory().getSwapTotal();
        // 磁盘总大小
        long totalDisk = 0;
        FileSystem fileSystem = systemInfo.getOperatingSystem().getFileSystem();
        for (OSFileStore fileStore : fileSystem.getFileStores()) {
            totalDisk = totalDisk + fileStore.getTotalSpace();
        }
        // JVM内存总大小
        long totalJvmMemory = Runtime.getRuntime().maxMemory();
        // 系统运行时间
        long systemStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();

        Map<String, Object> res = new HashMap<>();
        res.put("hostName", hostName);
        res.put("osName", osName);
        res.put("physicalProcessorCount", physicalProcessorCount);
        res.put("totalMemory", totalMemory);
        res.put("totalSwap", totalSwap);
        res.put("totalDisk", totalDisk);
        res.put("totalJvmMemory", totalJvmMemory);
        res.put("systemWorkDuration", System.currentTimeMillis() - systemStartTime);
        return Result.success(res);
    }

    @HasAuth("server-monitor")
    @PostMapping("/system/monitor")
    public Result monitor() throws InterruptedException {
        SystemInfo systemInfo = new SystemInfo();
        // cpu当前使用
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        Thread.sleep(1000);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long user =
                ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long nice =
                ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long sys =
                ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long idle =
                ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long iowait =
                ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long irq =
                ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] -
                prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal =
                ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal;
        // 内存可用
        long availableMemory = systemInfo.getHardware().getMemory().getAvailable();
        // swap已用
        long usedSwap = systemInfo.getHardware().getMemory().getVirtualMemory().getSwapUsed();
        // jvm当前使用
        long freeJvmMemory = Runtime.getRuntime().freeMemory();
        long maxJvmMemory = Runtime.getRuntime().maxMemory();
        // 磁盘当前使用
        long availableDisk = 0;
        FileSystem fileSystem = systemInfo.getOperatingSystem().getFileSystem();
        for (OSFileStore fileStore : fileSystem.getFileStores()) {
            availableDisk = availableDisk + fileStore.getUsableSpace();
        }
        Map<String, Object> res = new HashMap<>();
        res.put("totalCpu", totalCpu);
        res.put("usedCpu", user + sys);
        res.put("usedSwap", usedSwap);
        res.put("availableMemory", availableMemory);
        res.put("freeJvmMemory", freeJvmMemory);
        res.put("availableDisk", availableDisk);
        return Result.success(res);
    }
}
